package com.macro.mall.service.impl;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.macro.mall.common.util.StringUtil;
import com.macro.mall.dto.BmsMuseumApplicantDetail;
import com.macro.mall.dto.BmsMuseumApplicantParam;
import com.macro.mall.mapper.BmsMuseumApplicantMapper;
import com.macro.mall.mapper.BmsMuseumTeamMapper;
import com.macro.mall.model.BmsMuseumApplicant;
import com.macro.mall.model.BmsMuseumTeam;
import com.macro.mall.model.BmsTeamMuseumApplicant;
import com.macro.mall.service.BmsMuseumApplicantService;
import com.macro.mall.utils.ExcelUtils;
import org.apache.commons.lang3.StringUtils;

@Service
public class BmsMuseumApplicantServiceImpl implements BmsMuseumApplicantService {
	
	private static final Logger log = LoggerFactory.getLogger(BmsMuseumApplicantServiceImpl.class);

    @Autowired
    private BmsMuseumApplicantMapper bmsMuseumApplicantMapper;
    @Autowired
    private BmsMuseumTeamMapper bmsMuseumTeamMapper;

    @Override
    public List<BmsMuseumApplicant> getList(BmsMuseumApplicantParam bmsMuseumApplicantParam) {
        return bmsMuseumApplicantMapper.findList(bmsMuseumApplicantParam);
    }

    @Override
    public BmsMuseumApplicantDetail getItem(Long id) {
        BmsMuseumApplicant bmsMuseumApplicant = bmsMuseumApplicantMapper.selectByPrimaryKey(id);
        BmsMuseumApplicantDetail detail = new BmsMuseumApplicantDetail();
        BeanUtils.copyProperties(bmsMuseumApplicant, detail);
        return detail;
    }

    /**
     * 根据团队id修改人员报名状态
     */
    @Override
    @Transactional(rollbackForClassName = {})
    public int updateStatusByMuseumTeamId(BmsMuseumApplicant bmsMuseumApplicant){
        if(bmsMuseumApplicant.getMuseumTeamId()==null){
            return -1;
        }
        BmsMuseumTeam bmt =new BmsMuseumTeam();
        bmt.setId(bmsMuseumApplicant.getMuseumTeamId());
        bmt.setStatus(bmsMuseumApplicant.getStatus());
        bmsMuseumTeamMapper.updateByPrimaryKeySelective(bmt);
        return bmsMuseumApplicantMapper.updateStatusByMuseumTeamId(bmsMuseumApplicant);
    }
    
    @Override
    public void getExporttList(BmsMuseumApplicantParam bmsMuseumApplicantParam, HttpServletResponse response) {
    	String applicationType = bmsMuseumApplicantParam.getApplicationType();
    	if (StringUtils.equals("01", applicationType)) {
        	List<BmsMuseumApplicant> list = bmsMuseumApplicantMapper.findList(bmsMuseumApplicantParam);
        	try {
                String fileName = URLEncoder.encode("个人预约.xlsx", "UTF-8");
                response.setCharacterEncoding("utf-8");
                ExcelUtils.write(response, fileName, "个人预约", list, BmsMuseumApplicant.class);
    		} catch (Exception e) {
    			log.error("导出Excel文件时发生错误", e);
    		}
    	} else if (StringUtils.equals("02", applicationType)) {
        	List<BmsMuseumApplicant> list = bmsMuseumApplicantMapper.findList(bmsMuseumApplicantParam);
        	
        	String teamName = bmsMuseumApplicantParam.getName();
        	
        	List<BmsTeamMuseumApplicant> teamList = new ArrayList<>();
        	
        	for (BmsMuseumApplicant bmsMuseumApplicant : list) {
        		BmsTeamMuseumApplicant bmsTeamMuseumApplicant = new BmsTeamMuseumApplicant();
        		BeanUtils.copyProperties(bmsMuseumApplicant, bmsTeamMuseumApplicant);
        		bmsTeamMuseumApplicant.setTeamName(teamName);
        		teamList.add(bmsTeamMuseumApplicant);
        	}
        	
        	try {
                String fileName = URLEncoder.encode("团队预约.xlsx", "UTF-8");
                response.setCharacterEncoding("utf-8");
                ExcelUtils.write(response, fileName, "团队预约", teamList, BmsTeamMuseumApplicant.class);
    		} catch (Exception e) {
    			log.error("导出Excel文件时发生错误", e);
    		}
    	}

    }


    
}
